/**
 * text edit
 */
Marked.tools["bold"] = (function() {
    return {
        "name": "bold",
        "shortcut": "CTRL + B",
        "title": "${tool.bold}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<b>粗体</b> ", 3, -4);
                return;
            }
            com.skin.util.DOM.replace(textarea, "<b>" + content + "</b>", 3, -4);
        }
    };
})();

Marked.tools["italic"] = (function() {
    return {
        "name": "italic",
        "shortcut": "CTRL + I",
        "title": "${tool.italic}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<i>斜体</i> ", 3, -4);
                return;
            }
            com.skin.util.DOM.replace(textarea, "<i>" + content + "</i>", 3, -4);
        }
    };
})();

Marked.tools["header"] = (function() {
    return {
        "name": "header",
        "shortcut": "CTRL + H",
        "title": "${tool.header}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<h2>标题</h2>", 4, -5);
                return;
            }
            com.skin.util.DOM.replace(textarea, "<h2>" + content + "</h2>", 4, -5);
        }
    };
})();

Marked.tools["link"] = (function() {
    return {
        "name": "link",
        "shortcut": "CTRL + L",
        "title": "${tool.link}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<a href=\"http://\">http://</a>", 9, 16);
                return;
            }
            com.skin.util.DOM.replace(textarea, "<a href=\"http://\">" + content + "</a>", 9, 16);
        }
    };
})();

Marked.tools["image"] = (function() {
    return {
        "name": "image",
        "title": "${tool.image}",
        "click": function(editor) {
            Marked.UploadDialog.open(editor);
        },
        "execute": function(editor, url, title, select) {
            var textarea = editor.getTextarea();
            var content = "<img src=\"" + url + "\" title=\"" + title + "\"/>";

            if(!select) {
                com.skin.util.DOM.replace(textarea, content, content.length, content.length);
            }
            else {
                com.skin.util.DOM.replace(textarea, content, 10, 10 + url.length);
            }
        }
    };
})();

Marked.tools["emot"] = (function() {
    return {
        "name": "emot",
        "title": "${tool.emot}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            com.skin.util.DOM.replace(textarea, " :smile: ", 9, 9);
        }
    };
})();

Marked.tools["ulist"] = (function() {
    return {
        "name": "ulist",
        "title": "${tool.ulist}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<ul>\n<li>这里是列表文本</li>\n</ul>\n", 9, 16);
                return;
            }

            var b = ["<ul>"];
            var a = content.split("\n");

            for(var i = 0; i < a.length; i++) {
                b[b.length] = "<li>" + a[i] + "</li>";
            }

            b[b.length] = "</ul>";
            com.skin.util.DOM.replace(textarea, b.join(""), 9, 9);
        }
    };
})();

Marked.tools["olist"] = (function() {
    return {
        "name": "olist",
        "title": "${tool.olist}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<ol>\n<li>这里是列表文本</li>\n</ol>\n", 9, 16);
                return;
            }

            var b = ["<ol>"];
            var a = content.split("\n");

            for(var i = 0; i < a.length; i++) {
                b[b.length] = "<li>" + a[i] + "</li>";
            }

            b[b.length] = "</ol>";
            com.skin.util.DOM.replace(textarea, b.join(""), 9, 9);
        }
    };
})();

Marked.tools["hr"] = (function() {
    return {
        "name": "hr",
        "title": "${tool.hr}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            com.skin.util.DOM.replace(textarea, "<hr/>", 5, 5);
        }
    };
})();

Marked.tools["code"] = (function() {
    return {
        "name": "code",
        "title": "${tool.code}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<pre><code>这里输入代码</code></pre>", 11, 17);
                return;
            }
            com.skin.util.DOM.replace(textarea, "<pre><code>" + content + "</code></pre>", 11, 17);
        }
    };
})();

Marked.tools["quote"] = (function() {
    return {
        "name": "quote",
        "title": "${tool.quote}",
        "execute": function(editor) {
            var textarea = editor.getTextarea();
            var content = com.skin.util.DOM.getSelected(textarea);

            if(content == null || content.length < 1) {
                com.skin.util.DOM.replace(textarea, "<blockquote>这里输入引用文本</blockquote>", 12, 20);
                return;
            }
            com.skin.util.DOM.replace(textarea, "<blockquote>" + content + "</blockquote>", 12, 12 + content.length);
        }
    };
})();

Marked.tools["preview"] = (function() {
    return {
        "name": "preview",
        "title": "${tool.preview}",
        "execute": function(editor) {
            editor.fullscreen();
        }
    };
})();

Marked.tools["fullscreen"] = (function() {
    return {
        "name": "fullscreen",
        "title": "${tool.fullscreen}",
        "execute": function(editor) {
            editor.fullscreen();
        }
    };
})();

/**
 * 自定义插件
 */
Marked.plugin({name: "cs-plugin1", "execute": function(editor) {
    /**
     * 覆盖渲染方法
     */
    editor.render = function() {
        var t = this.getTextarea();
        var v = this.getMarkView();
        v.innerHTML = "<div style=\"margin: 0px 0px 0px 20px; padding: 0px 0px 0px 10px; border-left: 1px dashed #bbbbbb; overflow: hidden;\">" + t.value + "</div>";
    };
}});

